<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <div class="login-title">后台管理系统登录</div>
      </template>
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名"/>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码"/>
        </el-form-item>
        <el-form-item label="验证码" prop="captcha">
          <div class="captcha-container">
            <el-input v-model="loginForm.captcha" placeholder="请输入验证码" style="width: 60%"/>
            <img :src="captchaImage" @click="getCaptcha" class="captcha-image" title="点击刷新验证码"/>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin" :loading="loading">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '../store/user'
import api from '../api'

export default {
  name: 'Login',
  setup() {
    const router = useRouter()
    const userStore = useUserStore()
    const loginFormRef = ref(null)
    const loading = ref(false)
    const captchaKey = ref('')
    const captchaImage = ref('')
    
    const loginForm = ref({
      username: '',
      password: '',
      captcha: ''
    })
    
    const rules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
      ],
      captcha: [
        { required: true, message: '请输入验证码', trigger: 'blur' }
      ]
    }
    
    // 获取验证码
    const getCaptcha = async () => {
      try {
        const response = await api.get('/chance/auth/captcha')
        captchaKey.value = response.captcha_key
        captchaImage.value = response.image
      } catch (error) {
        ElMessage.error('获取验证码失败')
      }
    }
    
    // 登录处理
    const handleLogin = async () => {
      await loginFormRef.value.validate(async (valid) => {
        if (valid) {
          loading.value = true
          try {
            await userStore.login(
              loginForm.value.username,
              loginForm.value.password,
              loginForm.value.captcha,
              captchaKey.value
            )
            ElMessage.success('登录成功')
            router.push('/')
          } catch (error) {
            ElMessage.error(error.response?.data?.detail || '登录失败')
            getCaptcha() // 重新获取验证码
          } finally {
            loading.value = false
          }
        }
      })
    }
    
    onMounted(() => {
      getCaptcha()
    })
    
    return {
      loginForm,
      rules,
      loginFormRef,
      loading,
      captchaImage,
      handleLogin,
      getCaptcha
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

.login-card {
  width: 400px;
}

.login-title {
  text-align: center;
  font-size: 18px;
  font-weight: bold;
}

.captcha-container {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
}

.captcha-image {
  width: 38%;
  cursor: pointer;
  height: 50px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  object-fit: contain;
  background-color: #fff;
}
</style>